// JavaScript Document

var data={
    win_w: $(window).width(),
    win_h: $(window).height(),
    head:$('header'),
    foot:$('footer'),
    home:$('#home'),
    pro:$('#product'),
    Tpage:0,
    move:null,
    isAni:false,
    ison1:false
};

var init=function(){
    if(window.base.isPc){
        initFn.nav_menu();
        initFn.log();
    }
};


$(document).ready(function(){
    init();
    new alan.scrollAnimate();
    if(data.home.length>0){
        var home_next = function(){
            var items            = document.querySelectorAll('.row'),
                itemsCount       = items.length;
            if(data.Tpage<(itemsCount-1)){
                if( data.isAni) return false;
                data.isAni = true;
                var wid = 100/3*(data.Tpage+2);
                $('.precess').css('width',wid+'%');
                _navigate( 'next' ,items,itemsCount);
            }
        };
        var home_prev =function(){
            var items            = document.querySelectorAll('.row'),
                itemsCount       = items.length;
            if(data.Tpage>0){
                if( data.isAni) return false;
                data.isAni = true;
                var wid = 100/3*(data.Tpage);
                $('.precess').css('width',wid+'%');
                _navigate( 'prev' ,items,itemsCount);
            }
        };
        if(window.base.isPc){
            $('#home').mousewheel(function(event) {
                if(event.deltaY == -1){
                    home_next();
                }
                else if(event.deltaY == 1){
                    home_prev();
                }
            });
        }
        if(!window.base.isPc){
            $('#top').css('height',(data.win_h));
            $('#btm').css('height',(data.win_h));
            window.base.picMove();
        }
        homeFn.clock();
        homeFn.items();
        // homeFn.other_pop();
        homeFn._play();
        $('.node').addClass('on');
    }
    if(data.pro.length>0){
        proFn.tab();
        if(!window.base.isPc){
            data.head.css('background-color','#000');
        }
    }
});


$(window).resize(function() {

});

var initFn={
    log:function () {
        $('.search-show').on('click',function (object) {
            $('.searchGo').addClass('on');
        });
        $('.search-close').on('click',function (object) {
            $('.searchGo').removeClass('on');
        });
        $('#search-txt').keyup(function(event){
            var _this =$(this);
            if(event.keyCode ==13){
                if(_this.val()!=''){
                    var str = "search.php?keywords="+_this.val();
                    window.location.href=str;
                }
            }
        });
        if($('.music-set').hasClass('on')){
            $('#sound')[0].pause();
        }else{
            $('#sound')[0].play();
        }
        $('.music-set').on('click',function (object) {
            if($(this).hasClass('on')){
                $('#sound')[0].play();
            }else{
                $('#sound')[0].pause();
            }
            $(this).toggleClass('on');
        });
    },
    menu:function () {
        var DOM = {};
        var navType = false;
        DOM.intro = document.querySelector('#menu');
        DOM.shape1 = DOM.intro.querySelector('svg.shape1');
        DOM.shape2 = DOM.intro.querySelector('svg.shape2');
        DOM.path1 = DOM.shape1.querySelector('path');
        DOM.d1    = DOM.path1.getAttribute('d');
        DOM.path2 = DOM.shape2.querySelector('path');
        DOM.d2    = DOM.path2.getAttribute('d');
        DOM.enter = $('.enter');
        DOM.inner = $('.nav-inner');
        DOM.enter.on('click',function () {
            var _this = $(this);
            if(navType){
                nav_move(2);
                _this.removeClass('on');
                if(data.ison1){
                    data.head.addClass('on1');
                }
                navType = false;
            }else{
                nav_move(1);
                _this.addClass('on');
                if(data.ison1){
                    data.head.removeClass('on1');
                }
                navType = true;
            }
        });
        function nav_move(type) {
            if(type==1){
                anime({
                    targets: DOM.intro,
                    translateY: {
                        value: '-103vh',
                        delay: 100,
                        duration: 1200,
                        easing: 'easeInOutQuad'
                    },
                    begin: function() {
                        $('.wrapper').addClass('on');
                        disabledMouseWheel();
                    },
                    complete:function () {
                        $(DOM.intro).addClass('on');
                    }
                });
                anime({
                    targets: DOM.path1,
                    duration: 800,
                    easing: 'linear',
                    d: DOM.path1.getAttribute('pathdata:id'),
                    direction: 'alternate',
                });
            }
            if(type==2){
                anime({
                    targets: DOM.intro,
                    translateY: {
                        value: '-206vh',
                        delay: 100,
                        duration: 1200,
                        easing: 'easeInOutQuad'
                    },
                    begin: function() {
                        $('.wrapper').removeClass('on');
                        cancelDisMouseWheel();
                    }
                });
                anime({
                    targets: DOM.path2,
                    duration: 800,
                    easing: 'linear',
                    d: DOM.path2.getAttribute('pathdata:id'),
                    direction: 'alternate',
                    complete: function() {
                        $(DOM.intro).removeClass('on');
                        setTimeout(function () {
                            $(DOM.intro).css('transform','translateY(0)');
                        },500);
                    }
                });
            }
        }
    },
    nav_menu:function () {
        var _box = $('#nav-dialog'),
            isopen = false,
            mask = _box.find('.dialog_mask'),
            btn  = $('.go-menu');
        btn.on('click',function () {
            if(isopen){
                btn.removeClass('on');
                if(data.ison1){
                    data.head.addClass('on1');
                }
            }else{
                btn.addClass('on');
                if(data.ison1){
                    data.head.removeClass('on1');
                }
            }
            isopen = !isopen;
        });
        mask.on('click',function () {
            btn.removeClass('on');
            if(data.ison1){
                data.head.addClass('on1');
            }
            isopen = false;
        });
    }
};
var homeFn={
    clock:function () {
        var parent = $('.indexBanner'),
            clock = $('#clock'),
            msgBox = parent.find('.msg-box'),
            msg = msgBox.find('ol.msg_1').children('li'),
            msg_2 = msgBox.find('ol.msg_2').children('li'),
            next  = $('.scene-nav--next'),
            _cur  = $('.scene-nav--cur'),
            _shi  = $('.time-el').find('.shi'),
            _fen  = $('.time-el').find('.fen'),
            num   = clock.find('.time-num'),
            cir   = clock.find('.time-cir'),
            box   = clock.find('.time-move');
        var _w    = box.width()*window.devicePixelRatio+5,
            _h    = box.height()*window.devicePixelRatio+5,
            r     = _w/2 - 3;
        var cv = document.createElement('canvas'),
            ctx = cv.getContext('2d');
        ctx.imageSmoothingEnabled = true;
        cv.width=_w;
        cv.height=_h;
        box.append(cv);
        var rad = Math.PI*2/360,
            ani = false,
            isclick = true,
            index = 1,
            m1 = 0,
            m2 = 0,
            v1 = 0.5,
            v2 = 1;
        var once = true;
        var once2 = true;

        cir.find('li').on('click',function () {
            if(ani) return false;
            var cur = index%5,
                len = $(this).index();
            _cur.attr('data-nav',len);
            isclick = false;
            var a = (cur-len)>0 ? (5-(cur-len)) : (len-cur);
            if(a!=0){
                index = index+a;
            }
            _shi.addClass('on');
            v1=(v1*(20+5*a));
            v2=(v2*(a+1));
        });

        //绘制运动圆环
        function foregroundCircle(n,m){
            ctx.save();
            ctx.translate(_w/2,_h/2);
            ctx.strokeStyle = 'rgba(3,110,183,1)';
            ctx.lineWidth = 5;
            ctx.lineCap = "round";
            ctx.beginPath();
            ctx.arc(0, 0, r , Math.PI/2 +m*rad, Math.PI/2 +n*rad, false); //用于绘制圆弧context.arc(x坐标，y坐标，半径，起始角度，终止角度，顺时针/逆时针)
            ctx.stroke();
            ctx.closePath();
            ctx.restore();
        }
        //6段动画(改5)
        function move() {
            m1+=v1;
            if(once&&m1>=(50+72*(index-1))){
                once = false;
                _shi.css('transform','rotate('+(72*index)+'deg)');
                _fen.addClass('on');
            }
            if(m1>=(72*index)){
                m1 = 72*index;
                cir.find('li').eq(index%5).addClass('on').siblings().removeClass('on');
                num.find('li').eq(index%5).addClass('on').siblings().removeClass('on');
                msg.eq(index%5).addClass('on').siblings().removeClass('on');
                msg_2.eq(index%5).addClass('on').siblings().removeClass('on');
                $('.pic-box img').removeClass('on').eq(index%5).addClass('on');
                $('#home .msg-box .txt li').removeClass('on').eq(index%5).addClass('on');
                if(isclick){
                    if(once2){
                        next.click();
                        once2 = false;
                    }
                }else{
                    _cur.click();
                }
                ani = true;
                m2 +=v2;
                if(m2>=m1){
                    m2 = 72*index;
                    once=true;ani = false;isclick = true;once2=true;
                    setTimeout(function () {
                        _shi.removeClass('on');
                        _fen.removeClass('on');
                    },500);
                    v1 = 0.5;
                    v2 = 1;
                    index++;
                }
            }
            foregroundCircle(m1+180,m2+180);
        }
        function drawFrame(){
            window.requestAnimationFrame(drawFrame,cv);
            ctx.clearRect(0,0,cv.width,cv.height);
            move();
        }
        drawFrame();
    },
    other_pop:function () {
        var DOM = {};
        DOM.intro = document.querySelector('#pop');
        DOM.shape1 = DOM.intro.querySelector('svg.shape1');
        DOM.shape2 = DOM.intro.querySelector('svg.shape2');
        DOM.path1 = DOM.shape1.querySelector('path');
        DOM.d1    = DOM.path1.getAttribute('d');
        DOM.path2 = DOM.shape2.querySelector('path');
        DOM.d2    = DOM.path2.getAttribute('d');
        DOM.enter = $('.enter-pop');
        DOM.return = $('.return');
        DOM.enter.on('click',function () {
            var _pop = $('#'+$(this).attr('data-pop'));
            _pop.addClass('on').siblings().removeClass('on');
            nav_move(1);
            data.ison1 = true;
        });
        DOM.return.on('click',function () {
            nav_move(2);
            data.ison1 = false;
        });
        function nav_move(type) {
            if(type==1){
                anime({
                    targets: DOM.intro,
                    translateY: {
                        value: '-103vh',
                        delay: 100,
                        duration: 1200,
                        easing: 'easeInOutQuad'
                    },
                    begin: function() {
                        data.foot.addClass('on1');
                        data.head.addClass('on1');
                    },
                    complete:function () {
                        $(DOM.intro).addClass('on');
                    }
                });
                anime({
                    targets: DOM.path1,
                    duration: 800,
                    easing: 'linear',
                    d: DOM.path1.getAttribute('pathdata:id'),
                    direction: 'alternate',
                });
            }
            if(type==2){
                anime({
                    targets: DOM.intro,
                    translateY: {
                        value: '-206vh',
                        delay: 100,
                        duration: 1200,
                        easing: 'easeInOutQuad'
                    },
                    begin: function() {
                        data.foot.removeClass('on1');
                        data.head.removeClass('on1');
                    }
                });
                anime({
                    targets: DOM.path2,
                    duration: 800,
                    easing: 'linear',
                    d: DOM.path2.getAttribute('pathdata:id'),
                    direction: 'alternate',
                    complete: function() {
                        $(DOM.intro).removeClass('on');
                        setTimeout(function () {
                            $(DOM.intro).css('transform','translateY(0)');
                        },500);
                    }
                });
            }
        }
    },
    items:function () {
        var _parent = $('.other-pop');
        _parent.each(function () {
            var _item = $(this).find('.items'),
                _list = _item.find('.box'),
                next = _list.find('.next');
            var arr = ['b1','b2','b3','b4'],
                cur = 0,
                nextCur = 0,
                newArr = null,
                all = _list.length;
            var _next = function() {
                nextCur = cur<(all-1)?(cur+1):0;
                _list.eq(nextCur).siblings().removeClass('on');
                setTimeout(function () {
                    _list.eq(nextCur).addClass('on');
                },800);
                switch (nextCur){
                    case 0:
                        newArr = ['b1','b2','b3','b4'];
                        break;
                    case 1:
                        newArr = ['b4','b1','b2','b3'];
                        break;
                    case 2:
                        newArr = ['b3','b4','b1','b2'];
                        break;
                    case 3:
                        newArr = ['b2','b3','b4','b1'];
                        break;
                }
                _list.each(function (index) {
                    var _this = $(this);
                    _this.addClass(newArr[index]);
                    _this.removeClass(arr[index]);
                });
                arr = newArr;
                cur = nextCur;
            }
            var _prev = function() {
                nextCur = cur>0?(cur-1):(all-1);
                _list.eq(nextCur).siblings().removeClass('on');
                setTimeout(function () {
                    _list.eq(nextCur).addClass('on');
                },800);
                switch (nextCur){
                    case 0:
                        newArr = ['b1','b2','b3','b4'];
                        break;
                    case 1:
                        newArr = ['b4','b1','b2','b3'];
                        break;
                    case 2:
                        newArr = ['b3','b4','b1','b2'];
                        break;
                    case 3:
                        newArr = ['b2','b3','b4','b1'];
                        break;
                }
                _list.each(function (index) {
                    var _this = $(this);
                    _this.addClass(newArr[index]);
                    _this.removeClass(arr[index]);
                });
                arr = newArr;
                cur = nextCur;
            }
            next.on('click',function () {
                _next();
            });
            window.base.touchMove(_list,_next,_prev);
        });
    },
    _play:function () {
        $('.play').each(function () {
           var _this = $(this),
               video = $('#'+_this.attr('data-play'));
           _this.on('click',function () {
               _this.addClass('on');
               video.addClass('on');
               video[0].play();
           });
            video.on('click',function () {
                _this.removeClass('on');
                video.removeClass('on');
                video[0].pause();
            })
        });
    }
};
var proFn={
    tab:function () {
        var _tab = $('.tab'),
            open = false,
            ani = false,
            _down = _tab.find('.tab-down'),
            _child = _tab.find('.child_1');
        _down.on('click',function () {
            if(ani) return false;
            ani = true;
            if(open){
                _child.removeClass('fadeInRightSmall').addClass('fadeOutRightSmall');
                setTimeout(function () {
                    _child.hide();
                    _child.removeClass('fadeOutRightSmall').addClass('fadeInRightSmall');
                    ani = false;
                },600);
            }else{
                _child.show();
                setTimeout(function () {
                    ani = false;
                },600);
            }
            $(this).toggleClass('on');
            open = !open;
        })
    }
};

function _navigate( dir ,items,itemsCount) {
    var animEndEventNames = {
            'WebkitAnimation' : 'webkitAnimationEnd',
            'OAnimation' : 'oAnimationEnd',
            'msAnimation' : 'MSAnimationEnd',
            'animation' : 'animationend'
        },
        // animation end event name
        support              = { animations : Modernizr.cssanimations },
        animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];

    data.isAni = true;
    var cntAnims = 0;

    var currentItem = items[ data.Tpage ];

    if( dir === 'next' ) {
        data.Tpage = data.Tpage < itemsCount - 1 ? data.Tpage + 1 : 0;
    }
    else if( dir === 'prev' ) {
        data.Tpage = data.Tpage > 0 ? data.Tpage - 1 : itemsCount - 1;
    }
    else if( dir === 'num' ){
        data.Tpage = data.choosePage;
    }
    if(data.Tpage==1){data.foot.addClass('on');data.head.addClass('on');}else{data.foot.removeClass('on');data.head.removeClass('on');}
    if(data.Tpage==2){$('.direction').addClass('up');}else{$('.direction').removeClass('up');}
    var nextItem = items[ data.Tpage ];
    function a() {
        classie.removeClass( currentItem, 'on' );
        classie.removeClass( currentItem, dir === 'prev' ? 'navOutPrev' : 'navOutNext' );
        ++cntAnims;
        if( cntAnims === 2 ) {
            setTimeout(function () {
                data.isAni = false;
            },100);
        }
    }
    function b() {
        classie.addClass( nextItem, 'on' );
        classie.removeClass( nextItem, dir === 'prev' ? 'navInPrev' : 'navInNext' );
        ++cntAnims;
        if( cntAnims === 2 ) {
            setTimeout(function () {
                data.isAni = false;
            },100);
        }
    }
    if( support.animations ) {
        classie.addClass( currentItem, dir === 'prev' ? 'navOutPrev' : 'navOutNext' );
        classie.addClass( nextItem, dir === 'prev' ? 'navInPrev' : 'navInNext' );
        setTimeout(function () {
            a();
            b();
        },700);
    }
    else {
        console.log('不支持css3 animated');
        $(currentItem).hide();
        $(nextItem).fadeIn(200);
        setTimeout(function(){
            classie.removeClass( currentItem, 'on' );
            classie.addClass( nextItem, 'on' );
            data.isAni = false;
        },200)
    }
}

//阻止浏览器事件
function disabledMouseWheel() {
    document.addEventListener('DOMMouseScroll', scrollFunc, false);
    document.addEventListener('mousewheel',scrollFunc,false);
}
//取消阻止浏览器事件
function cancelDisMouseWheel(){
    document.removeEventListener('DOMMouseScroll',scrollFunc,false);
    document.removeEventListener('mousewheel',scrollFunc,false);
}
function scrollFunc(evt) {
    evt = evt || window.event;
    if(evt.preventDefault) {
        // Firefox
        evt.preventDefault();
        evt.stopPropagation();
    } else{
        // IE
        evt.cancelBubble=true;
        evt.returnValue = false;
    }
    return false;
}










